﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UniRlv.Utility
{
    public static class Extensions
    {
        /// <summary>
        /// 字典列表的扩展方法
        /// </summary>
        /// <param name="dictList">字典列表</param>
        /// <returns>转换为DataTable</returns>
        public static DataTable ToDataTable(this List<Dictionary<string, object>> dictList)
        {
            var tbl = new DataTable();
            DataColumn col;
            DataRow row;
            foreach (var x in dictList)
            {
                row = tbl.NewRow();
                foreach (var k in x.Keys)
                {
                    col = tbl.Columns[k];
                    if (col == null)
                        col = tbl.Columns.Add(k, typeof(string));
                    row[col] = x[k];
                }
                tbl.Rows.Add(row);
            }
            return tbl;
        }

        public static void AppendDictionary(this Dictionary<string, object> dict, Dictionary<string, object> dict2)
        {

            if (dict2 == null)
            {
                return;
            }

            if (dict == null)
            {
                dict = new Dictionary<string, object>();
            }

            foreach (string item in dict2.Keys)
            {
                dict[item] = dict2[item];
            }
        }

        public static Dictionary<string, object> ToDictionary(this DataRow dr)
        {
            if (dr == null)
            {
                return null;
            }

            Dictionary<string, object> dict = new Dictionary<string, object>();
            DataColumnCollection cols = dr.Table.Columns;
            for (int i = 0; i < cols.Count; i++)
            {
                dict[cols[i].ColumnName] = dr[i];
            }

            return dict;
        }

        public static void SetRowSelect(this DataGridView dgv, int rowIndex)
        {
            if (rowIndex>=0 && rowIndex< dgv.Rows.Count)
            {
                dgv.Rows[rowIndex].Selected = true;
                dgv.CurrentCell = dgv.Rows[rowIndex].Cells[dgv.Columns.Count - 1];
            }
            else
            {
                foreach (DataGridViewRow item in dgv.Rows)
                {
                    item.Selected = false;
                }
            }
        }

        public static DataTable ToTable(this DataGridView dgv)
        {
            DataTable dt = new DataTable();

            // 列强制转换
            for (int count = 0; count < dgv.Columns.Count; count++)
            {
                if (dgv.Columns[count].Visible)
                {//隐藏的不导出
                    DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
                    dt.Columns.Add(dc);
                }

            }

            // 循环行
            int cols = 0;
            for (int count = 0; count < dgv.Rows.Count; count++)
            {
                DataRow dr = dt.NewRow();
                cols = 0;
                for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
                {
                    if (dgv.Columns[countsub].Visible)
                    {
                        dr[cols] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
                        cols++;
                    }
                    
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
}
